package com.lvy.hczn.transmit.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.core.controller.BaseController;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.Warehouse;
import com.lvy.hczn.front.system.domain.system.SysArea;
import com.lvy.hczn.front.system.service.WarehouseService;
import com.lvy.hczn.mq.rocket.ParamInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/api/dt/wh/")
@RestController
@Slf4j
public class WarehouseController extends BaseController {

    @Autowired
    private WarehouseService warehouseService;

    /**
     * 获取仓库编号
     *
     * @param info
     * @return
     */
    @ApiOperation(value = "获取仓库编号", notes = "获取仓库编号")
    @PostMapping("code")
    @ResponseBody
    public AjaxResult generateWarehouseCode(HttpServletRequest request, @RequestBody ParamInfo<Warehouse> info) {
        try {
            String result = warehouseService.generateWarehouseCode(info);
            return AjaxResult.success("", result);
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("generateWarehouseCode同步异常：", e);
                return AjaxResult.error("系统异常");
            }
        }
    }

    /**
     * 空箱铺箱入库单
     *
     * @param info
     * @return
     */
    @ApiOperation(value = "总仓绑定运营区域", notes = "总仓绑定运营区域")
    @PostMapping("binding")
    @ResponseBody
    public AjaxResult bindingRegionInfo(HttpServletRequest request, @RequestBody List<ParamInfo> info) {
        try {
            warehouseService.bindingRegionInfo(request, info);
            return AjaxResult.success();
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("bindingRegionInfo同步异常：{}", e);
                return AjaxResult.error("系统异常");
            }
        }
    }


    @ApiOperation(value = "仓库初始化时，获取总仓列表", notes = "仓库初始化时，获取总仓列表")
    @PostMapping("parent")
    @ResponseBody
    public AjaxResult parentList(HttpServletRequest request, @RequestBody ParamInfo<Warehouse> info) {
        try {
            return AjaxResult.success(warehouseService.parentList(info));
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("parentList同步异常：{}", e);
                return AjaxResult.error("系统异常");
            }
        }
    }

    @ApiOperation(value = "修改仓库联系人，电话信息", notes = "修改仓库联系人，电话信息")
    @PostMapping("contacts")
    @ResponseBody
    public AjaxResult updateNameAndPhoneById(HttpServletRequest request, @RequestBody ParamInfo<Warehouse> info) {
        try {
            if (info == null || info.getData() == null) {
                throw new UtilException("参数为空");
            }
            Warehouse warehouse = JSONUtil.toBean(JSONUtil.toJsonStr(info.getData()), Warehouse.class);
            warehouseService.updateByIdFromPlatForm(warehouse);
            return AjaxResult.success();
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("updateNameAndPhoneById同步异常：{}", e);
                return AjaxResult.error("系统异常");
            }
        }
    }
}
